package com.hyj.ecp.backstage.interceptor;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.hyj.ecp.backstage.sys.bean.vo.User;
import com.hyj.ecp.common.constant.SysConstant;
import com.hyj.util.JsonUtil;

public class LoginInterceptor2 implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		 
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		 
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		 User u =(User) request.getSession().getAttribute("user");
	     System.out.println("LoginInterceptor2  update  save  delete ....."+u);
		    
		   if(u==null){
			    Map<String,Object> vo = new HashMap<String,Object>();
			    response.setContentType("application/json");
			    vo.put("returnCode", 999);
				vo.put("saveNum", 0);
				vo.put("msg", SysConstant.NOT_LOGIN);
				String json = JsonUtil.toJson(vo, false);
	    		OutputStream out =  response.getOutputStream();
				out.write(json.getBytes("UTF-8"));
				out.flush();
				out.close();
	    		
		   		return false;// 没有登入 
		   }
		   return true;
	}

}
